package com.test.javafaq_faq.kafka;

import com.google.gson.Gson;
import com.test.javafaq_commons.entity.Question;
import com.test.javafaq_commons.vo.Topic;
import lombok.extern.slf4j.Slf4j;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
@Slf4j
public class KafkaProducer {
    @Resource

    private KafkaTemplate<String,String> kafkaTemplate;

    private Gson gson = new Gson();

    //将question对象发送给kafka
    public void sendQuestion(Question question){
        log.debug("准备发送：{}",question);
        String json = gson.toJson(question);
        log.debug("发送信息：{}",json);
        kafkaTemplate.send(Topic.QUESTIONS,json);
    }

}
